package com.dmooo.ymx.mall;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dmooo.ymx.CaiNiaoApplication;
import com.dmooo.ymx.R;
import com.dmooo.ymx.base.BaseActivity;
import com.dmooo.ymx.config.Constants;
import com.dmooo.ymx.widget.CircleImageView;

import java.io.UnsupportedEncodingException;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShopMallMineActivity extends BaseActivity {
    @BindView(R.id.img_head)
    CircleImageView imgHead;
    @BindView(R.id.txt_name)
    TextView txtName;

    @Override
    protected void initUI() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        setContentView(R.layout.activity_shop_mall_mine);
        ButterKnife.bind(this);
        txtName.setText(CaiNiaoApplication.getUserInfoBean().user_detail.nickname==null?CaiNiaoApplication.getUserInfoBean().user_msg.phone:isBase64(CaiNiaoApplication.getUserInfoBean().user_detail.nickname)?base64Decode(CaiNiaoApplication.getUserInfoBean().user_detail.nickname,"utf-8"):CaiNiaoApplication.getUserInfoBean().user_detail.nickname);
        Glide.with(this).load(CaiNiaoApplication.getUserInfoBean().user_detail.avatar).placeholder(R.mipmap.logo_app).error(R.mipmap.logo_app).dontAnimate().into(imgHead);
    }
    private static boolean isBase64(String str) {
        String base64Pattern = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
        return Pattern.matches(base64Pattern, str);
    }
    /**
     * Base64解密字符串
     * @param content -- 待解密字符串
     * @param charsetName -- 字符串编码方式
     * @return
     */
    private String base64Decode(String content, String charsetName) {
        if (TextUtils.isEmpty(charsetName)) {
            charsetName = "UTF-8";
        }
        byte[] contentByte = Base64.decode(content, Base64.DEFAULT);
        try {
            return new String(contentByte, charsetName);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }
    @Override
    protected void initData() {
        findViewById(R.id.txt_one).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivity(MallGoodsCartActivity.class);
            }
        });
        findViewById(R.id.txt_two).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivity(AddressActivity.class);
            }
        });
        findViewById(R.id.txt_three).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
    }

    @Override
    protected void initListener() {

    }

    @OnClick({R.id.imgBack, R.id.my_zyallorder, R.id.my_zydfkorder, R.id.my_zydfhorder, R.id.my_zydshorder, R.id.my_zyywcorder})
    public void onViewClicked(View view) {
        Bundle bundle = new Bundle();
        switch (view.getId()) {
            case R.id.imgBack:
                finish();
                break;
            case R.id.my_zyallorder:
                bundle.putString("type", "0");
                Constants.MALL_ORDER_TYPE = "0";
                openActivity(MyShopMallOrderActivity.class, bundle);
                break;
            case R.id.my_zydfkorder:
                bundle.putString("type", "1");
                Constants.MALL_ORDER_TYPE = "0";
                openActivity(MyShopMallOrderActivity.class, bundle);
                break;
            case R.id.my_zydfhorder:
                bundle.putString("type", "2");
                Constants.MALL_ORDER_TYPE = "0";
                openActivity(MyShopMallOrderActivity.class, bundle);
                break;
            case R.id.my_zydshorder:
                bundle.putString("type", "3");
                Constants.MALL_ORDER_TYPE = "0";
                openActivity(MyShopMallOrderActivity.class, bundle);
                break;
            case R.id.my_zyywcorder:
                bundle.putString("type", "4");
                Constants.MALL_ORDER_TYPE = "0";
                openActivity(MyShopMallOrderActivity.class, bundle);
                break;
        }
    }
}
